package com.mvc.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.mvc.utility.WebConstants;

public class LoginInterceptor implements HandlerInterceptor {
	private static Logger log = Logger.getLogger(LoginInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//true 拦截
		//false 释放
		log.debug("preHandle called!");
		log.debug(request.getRequestURI());
		if(request.getRequestURI().equals(request.getContextPath() + "/user/doLogin.do"))
			return true;
		log.debug(request.getSession().getAttribute("username"));
		if(null !=request.getSession().getAttribute("username"))
			return true;
		else
		{
			response.sendRedirect(request.getContextPath() + "/user/doLogin.do");
			//request.setAttribute(WebConstants.REQUEST_PREURI, request.getRequestURI());
			return true;
		}
		
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}

	
}
